package com.ly.common.util;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.*;

public class SwtUtils {

	public static int msg(String message, Shell parent, int... styles) {
		int s = SWT.NONE;
		for (int style : styles) {
			s = s | style;
		}
		MessageBox mb = new MessageBox(parent, s);
		mb.setText("系统提示");
		mb.setMessage(message);
		return mb.open();
	}

	public static void center(Shell shell) {
		int x = Display.getCurrent().getBounds().width - shell.getBounds().width;
		int y = Display.getCurrent().getBounds().height - shell.getBounds().height;
		x *= 0.5;
		y *= 0.4;
		shell.setLocation(x, y);
	}

	public static void fullImg(Control c) {
		PaintListener pl = new PaintListener() {
			public void paintControl(PaintEvent e) {
				Image img;
				if (e.getSource() instanceof Label) {
					img = ((Label) e.getSource()).getImage();
				} else if (e.getSource() instanceof Button) {
					img = ((Button) e.getSource()).getImage();
				} else {
					img = c.getBackgroundImage();
				}
				if(img==null)return;
				double wr = (double)c.getBounds().width / (double)img.getBounds().width;
				double hr = (double)c.getBounds().height / (double)img.getBounds().height;
				double r = Math.max(wr, hr);
				int destWidth = (int) (img.getBounds().width * r);
				int destHeight = (int) (img.getBounds().height * r);
				e.gc.drawImage(img, 0, 0, img.getBounds().width, img.getBounds().height, 0, 0, destWidth, destHeight);
			}
		};
		c.addPaintListener(pl);
	}

}
